using UnityEngine;
using System.Collections;

public class Player : Character {

    public float movementSpeed = 1.0f;
	
	// animation-related variables
	public float idleAnimationSpeed = 0.18f;
	public float runningAnimationSpeed = 0.1f;
    public float attackingAnimationSpeed = 0.005f;
    public float dyingAnimationSpeed = 0.18f;
    protected bool isDead;

    public GameObject gameManager;

	// Use this for initialization
	void Start () {
		if (GlobalState.isRedHero) {
		} else {
            renderer.material = Resources.Load("blackheartMaterial") as Material;
		}
		
        InitializeAnimationList();
        AddAnimation("running", 0, 0, 3, runningAnimationSpeed);
        AddAnimation("attacking", 2, 0, 4, attackingAnimationSpeed);
        AddAnimation("idle", 0, 0, 3, idleAnimationSpeed);
        AddAnimation("dying", 1, 0, 3, dyingAnimationSpeed);
        AddAnimation("transforming", 0, 0, 27, .2f);

        StartAnimation("idle", true);

        //Ugh, inheritance problems, copying damagable::start...
        if (GlobalState.isRedHero)
            currentHP = totalHP;
        else
            currentHP = totalHP / 2;
	}
	
	// Update is called once per frame
	protected override void Update () {
	    //Move the player if you're holding down movement keys (and not attacking)
        if (gameManager.GetComponent<GameManager>().gameWin)
        {
            if (GetCurrentAnimationName() != "transforming")
            {
                renderer.material = Resources.Load("transformationMaterial") as Material;
                spriteSheetXSize = 28;
                spriteSheetYSize = 1;
                StartAnimation("transforming", false, !GlobalState.isRedHero);
            }
        }
        else if (!isAttacking && !isDead)
        {
            Vector3 deltaPos = new Vector3(0.0f, 0.0f, 0.0f);
            if (Input.GetKey(KeyCode.A))
                deltaPos.x -= movementSpeed * Time.deltaTime;
            if (Input.GetKey(KeyCode.D))
                deltaPos.x += movementSpeed * Time.deltaTime;
            if (Input.GetKey(KeyCode.W))
                deltaPos.z += movementSpeed * Time.deltaTime;
            if (Input.GetKey(KeyCode.S))
                deltaPos.z -= movementSpeed * Time.deltaTime;
            MoveCharacter(deltaPos);
			
			// Set the running animation state
			if ((deltaPos.x != 0.0f) || (deltaPos.y != 0.0f) || (deltaPos.z != 0.0f)) {
                if (GetCurrentAnimationName() != "running")
                    StartAnimation("running", true);
            }
            else
            {
                if (GetCurrentAnimationName() != "idle")
                    StartAnimation("idle", true);
			}
        }

        //Attack! (if not already attacking)
        if (!isAttacking && !isDead)
        {
            if (Input.GetKeyDown(KeyCode.Comma)) {
                switch (GetComponent<PlayerPowerups>().powerup)
                {
                    case PlayerPowerups.PowerupNames.HeartBeet:
                        Attack(1);
                        break;
                    default:
                        Attack(0);
                        break;
                }
                StartAnimation("attacking", false);
			}
        }
		
		// Animate the sprite
		//Animate();

        base.Update();
	}

    protected override void OnAnimationFinished(string name)
    {
        if (name == "dying")
        {
			Application.LoadLevel ("gameover");
            base.Die();
        }
        else if (name == "transforming")
        {
            gameManager.GetComponent<GameManager>().GoToWinScreen();
        }
    }

    protected override void Die()
    {
        collider.enabled = false;
        StartAnimation("dying");
        isDead = true;
    }

    public override void DoDamage(int damage)
    {
        base.DoDamage(damage);
        if (isDamagable)
            SoundEffects.instance.playPlayerDamage();
    }
}
